package customer.ejb.eaos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import common.Filter;
import common.IPredicate;
import model.GetEntities;
import model.Hotel;
import model.PredefinedTP;
import dtos.HotelDTO;
import dtos.PredefinedTPDTO;

@Stateless
public class PredefinedTPFilterEAO implements PredefinedTPFiltered{

	@PersistenceContext
    private EntityManager em;
	
	private String from;
	private String to;
	
	public PredefinedTPFilterEAO(){
		
	}

	@Override
	public void add(PredefinedTPDTO predefinedTPDTO) {
		GetEntities ge = new GetEntities();
		em.persist(ge.getPredefinedTPEntity(predefinedTPDTO));
		
	}

	@Override
	public Collection<PredefinedTPDTO> getTravelList(String from, String to) {
		this.from = from;
		this.to = to;
		
		@SuppressWarnings("unchecked")
		List<PredefinedTP> qres = em.createQuery("SELECT x FROM PredefinedTP x").getResultList();
		Collection<PredefinedTP> fres = Filter.filter(qres, isOK);
		Collection<PredefinedTPDTO> res = new ArrayList<PredefinedTPDTO>(fres.size());
		
		
		for(PredefinedTP e : fres)
		{
			res.add(e.getDTO());
		}
		
		return res;
		
	}
	
	IPredicate<PredefinedTP> isOK = new IPredicate<PredefinedTP>() {
		public boolean apply(PredefinedTP tp) {
	        return tp.isFromTo(from,to);
	    }

	};
	

	@Override
	public PredefinedTPDTO getPredefinedTPDTO(int id) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
}
